package com.gz.mylife.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gz.mylife.R;
import com.gz.mylife.util.ImageUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.zl.smartmall.library.im.po.MessageInfo;
import com.zl.smartmall.library.util.Constants;
import com.zl.smartmall.library.util.CrashHandler;

public class ChattingAdapter extends BaseAdapter {
	
	private static final String TAG = ChattingAdapter.class.getSimpleName();
	private DisplayImageOptions mPortraitOptions;
	private LayoutInflater mInflater;
	private List<MessageInfo> mMessages;
	
	public ChattingAdapter(Context context, List<MessageInfo> messages) {
		mInflater = LayoutInflater.from(context);
		mMessages = new ArrayList<MessageInfo>();
		mMessages.addAll(messages);
		mPortraitOptions = ImageUtils.buildCircleOptions();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mMessages.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mMessages.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return mMessages.get(position).getSendFrom();
	}
	
	@Override
	public int getViewTypeCount() {
		// TODO Auto-generated method stub
		return 2;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(convertView == null) {
			CrashHandler.d(TAG, TAG + "创建convertView");
			switch (getItemViewType(position)) {
			case Constants.IM_MSG_SEND_FROM_OTHER:
				convertView = mInflater.inflate(R.layout.chat_msgbox_receive, null);
				ReceiverViewHolder receiveHolder = new ReceiverViewHolder();
				receiveHolder.message = (TextView) convertView.findViewById(R.id.tv_message);
				receiveHolder.nickname = (TextView) convertView.findViewById(R.id.tv_nickname);
				receiveHolder.portrait = (ImageView) convertView.findViewById(R.id.iv_portrait);
				convertView.setTag(receiveHolder);
				break;

			case Constants.IM_MSG_SEND_FROM_ME:
				convertView = mInflater.inflate(R.layout.chat_msgbox_send, null);
				SendViewHolder sendHolder = new SendViewHolder();
				sendHolder.message = (TextView) convertView.findViewById(R.id.tv_message);
				sendHolder.portrait = (ImageView) convertView.findViewById(R.id.iv_portrait);
				convertView.setTag(sendHolder);
				break;
			}
		}
		
		switch (getItemViewType(position)) {
		case Constants.IM_MSG_SEND_FROM_OTHER:
			ReceiverViewHolder receiverHolder = (ReceiverViewHolder) convertView.getTag();
			receiverHolder.message.setText(mMessages.get(position).getMessage());
			receiverHolder.nickname.setText(mMessages.get(position).getSenderNickname());
			ImageLoader.getInstance().displayImage(mMessages.get(position).getSenderFace(), receiverHolder.portrait, mPortraitOptions);
			break;

		case Constants.IM_MSG_SEND_FROM_ME:
			SendViewHolder sendHolder = (SendViewHolder) convertView.getTag();
			sendHolder.message.setText(mMessages.get(position).getMessage());
			ImageLoader.getInstance().displayImage(mMessages.get(position).getSenderFace(), sendHolder.portrait, mPortraitOptions);
			break;
		}
		
		return convertView;
	}
	
	public void add(MessageInfo message) {
		mMessages.add(message);
		notifyDataSetChanged();
	}
	
	public void addAll(List<MessageInfo> messages) {
		mMessages.addAll(messages);
		notifyDataSetChanged();
	}
	
	public void addAll(List<MessageInfo> messages, int position) {
		mMessages.addAll(position, messages);
		notifyDataSetChanged();
	}
	
	public void clear() {
		mMessages.clear();
		notifyDataSetChanged();
	}
	
	static class ReceiverViewHolder {
		ImageView portrait;
		TextView nickname;
		TextView message;
	}
	
	static class SendViewHolder {
		ImageView portrait;
		TextView message;
	}

}
